package com.clayecloud.module_login.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.lib_base.utils.KeyboradUtil;
import com.clayecloud.module_login.R;
import com.clayecloud.module_login.adapter.AdapterFragmentPager;
import com.clayecloud.module_login.databinding.ActivityLoginBinding;
import com.clayecloud.module_login.ui.Fragment.AccountLoginFragment;
import com.clayecloud.module_login.ui.Fragment.PhoneLoginFragment;
import com.clayecloud.module_login.viewModels.LoginActivityViewModel;
import com.gyf.immersionbar.ImmersionBar;

@Route(path = "/login/account/index")
public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding activityLoginBinding;
    private LoginActivityViewModel loginActivityViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLoginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this, factory);
        loginActivityViewModel = viewModelProvider.get(LoginActivityViewModel.class);

        //加载数据
        loginActivityViewModel.loadData();
        activityLoginBinding.setViewModel(loginActivityViewModel);

        //控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();

        //登录页使用viewpager切换
        SparseArray<Fragment> fragments = new SparseArray<>();
        fragments.append(0, new AccountLoginFragment());
        fragments.append(1, new PhoneLoginFragment());
        AdapterFragmentPager adapterFragmentPager = new AdapterFragmentPager(this, fragments);
        activityLoginBinding.viewPager2.setAdapter(adapterFragmentPager);
        activityLoginBinding.viewPager2.setUserInputEnabled(false);   // 关闭左右滑动

        //观察者模式对显示页进行感知切换
        loginActivityViewModel.getCurrentPager().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                activityLoginBinding.viewPager2.setCurrentItem(integer, false);
            }
        });

        //观察登录的状态
        loginActivityViewModel.getBool_login().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    finish();
                    overridePendingTransition(R.anim.window_fake, R.anim.window_bottom_out);
                }
            }
        });
    }

    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }

    /**
     * 重写每次点击事件，并进行判断是不是editText
     * @param motionEvent
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (KeyboradUtil.isShouldHideInput(v, motionEvent)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(motionEvent);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(motionEvent)) {
            return true;
        }
        return onTouchEvent(motionEvent);
    }

}
